﻿// Author:					    Joe Audette
// Created:				        2011-03-14
// Last Modified:			    2011-03-14
// 
// The use and distribution terms for this software are covered by the 
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)  
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by 
// the terms of this license.
//
// You must not remove this notice, or any other, from this software.

using System;
using System.IO;
using System.Globalization;
using System.Text;
using System.Web;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Web.Framework;
using Ionic.Zip;

namespace mojoPortal.Web.DesignTools
{
    public partial class DownloadSkin : NonCmsBasePage
    {
        private string skinName = string.Empty;
        private string skinBasePath = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if ((!WebUser.IsInRoles(siteSettings.RolesThatCanManageSkins)))
            {
                SiteUtils.RedirectToAccessDeniedPage(this);
                return;
            }

            LoadSettings();

            if (skinName.Length > 0)
            {
                if(Directory.Exists(Server.MapPath(skinBasePath + skinName)))
                {
                    Download();
                }
                else
                {
                    lblMessage.Text = "Invalid skin name passed in reuest.";
                }
            }
            else
            {
                lblMessage.Text = "No skin name passed in reuest.";
            }

        }

        private void Download()
        {
            try
            {
                using (ZipFile zip = new ZipFile())
                {
                    zip.AddDirectory(Server.MapPath(skinBasePath + skinName));

                    Page.Response.ContentType = "application/zip";
                    Page.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(skinName + ".zip", Encoding.UTF8) + "\"");

                    //

                    zip.Save(Response.OutputStream);
                    

                }
            }
            catch (ZipException ex)
            {
                lblMessage.Text = ex.Message;
            }
        }

        private void LoadSettings()
        {
            skinBasePath = "~/Data/Sites/" + siteSettings.SiteId.ToInvariantString() + "/skins/";
            skinName = WebUtils.ParseStringFromQueryString("s", string.Empty);
        }


        #region OnInit
        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new EventHandler(this.Page_Load);

            SuppressMenuSelection();
            SuppressPageMenu();

        }
        #endregion
    }
}